﻿using WpfModelEditor.Core.Plugins;
using Fluent;
using HelixToolkit.Wpf.SharpDX;
using HelixToolkit.Wpf.SharpDX.Model.Scene;
using System.Diagnostics;
using MahApps.Metro.IconPacks;
using System.Windows;
using System;

namespace EditPlugin
{
    public class EditPluginImpl : IPlugin
    {
        private static EditPluginImpl _instance = null;
        public static EditPluginImpl Instance { get { return _instance; } }
        public EditPluginImpl()
        {
            Debug.Assert(_instance == null);
            _instance = this;
        }
        public int Order { get { return 20; } }
        public IHost Host { get; private set; }
        public void Initialize(IHost host)
        {
            Host = host;
            var tab = host.UI.GetOrCreateRibbonTabItem("开始");
            var group = new RibbonGroupBox
            {
                Header = "模式",
            };
            tab.Groups.Add(group);
            {
                var btn = new ToggleButton
                {
                    GroupName = "Mode",
                    ToolTip = "选择",
                    SizeDefinition = RibbonControlSizeDefinition.FromString("Middle"),
                    IsChecked = Host.Mode == "Select",
                };
                btn.Click += (o, e) => { host.Mode = "Select"; };
                group.Items.Add(btn);
            }
            {
                var btn = new ToggleButton
                {
                    GroupName = "Mode",
                    ToolTip = "编辑",
                    SizeDefinition = RibbonControlSizeDefinition.FromString("Middle"),
                };
                btn.Click += (o, e) => { host.Mode = "Edit"; };
                group.Items.Add(btn);
            }

            tab = host.UI.GetOrCreateRibbonTabItem("编辑");
            group = new RibbonGroupBox
            {
                Header = "优化",
            };
            tab.Groups.Add(group);
            {
                var btn = new Fluent.Button
                {
                    Header = "Gltf优化",
                    SizeDefinition = RibbonControlSizeDefinition.FromString("Large"),
                    LargeIcon = new PackIconMaterial { Kind = PackIconMaterialKind.OpenInNew },
                    Icon = new PackIconMaterial { Kind = PackIconMaterialKind.OpenInNew },
                };
                btn.Click += GltfOptButton_Click;
                group.Items.Add(btn);
            }
        }

        private void GltfOptButton_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new GltfOptimizer
            {
                Owner = Host.UI.Window
            };
            dlg.Show();
        }

        private void CheckManipulator()
        {
            if (Host.Mode == "Edit" && Host.SelectedObject != null)
            {
                var obj = Host.SelectedObject;
                if (obj is MeshNode)
                {
                    var node = obj as MeshNode;
                    //Host.Manipulator.Target = node.Geometry;
                }
                else if (obj is Element3D)
                {
                    Host.Manipulator.Target = obj as Element3D;
                }
                else
                {
                    Host.Manipulator.Target = null;
                }
                Host.Manipulator.IsRendering = Host.Manipulator.Target != null;
            }
            else
            {
                Host.Manipulator.IsRendering = false;
            }
        }
        public void OnEvent(string eventName, object data)
        {
            if (eventName == "ModeChanged")
            {
                CheckManipulator();
            }
            else if (eventName == "SelectedChanged")
            {
                CheckManipulator();
            }
            else if (eventName == "MouseUp")
            {
                var ev = data as MouseUp3DEventArgs;
                if (ev != null && !ev.Handled && Host.Mode == "Select" || Host.Mode == "Edit")
                {
                    Host.SelectedObject = ev.HitTestResult?.ModelHit;
                }
            }
        }

        public void Clearup()
        {
        }
    }
}